#!/bin/python3
import subprocess
import os
import io
from io import StringIO


wr = StringIO()

def command(path):
   str = "nslookup {}|grep Address|tail -n -1".format(path)
   out = subprocess.check_output(["/usr/bin/bash", "-c",str])
   
   tian = "{}".format(out)

   print(tian)
   return tian

def shellexe(ln):
   wr.write("{}:{}:{}:{}:5057:ff:feea:3111 ceph2\n".format(ln[1],ln[2],ln[3],ln[4]))
   wr.write("{}:{}:{}:{}:5057:ff:feea:f111 ceph1\n".format(ln[1],ln[2],ln[3],ln[4]))
   wr.write("{}:{}:{}:{}:5057:ff:fe7a:f111 ceph3\n".format(ln[1],ln[2],ln[3],ln[4]))
   wr.write("{}:{}:{}:{}:692:26ff:febd:c9a9 ceph\n".format(ln[1],ln[2],ln[3],ln[4]))
   wr.write("{}:{}:{}:{}:f816:8cdb:de5e:aad0 win10\n".format(ln[1],ln[2],ln[3],ln[4]))

def writefile():
    with open('/etc/hosts','+w') as tfile :
       tfile.write(wr.getvalue())


def readfile():
    file = open('/etc/hosts')
    re = StringIO(file.read())
    while True:
        line = re.readline()
        if line == '':
            break
        if line.find('ceph') <=  0:
           wr.write(line)
    file.close()
        

if __name__ == "__main__" :
   str=  command('home.sinzuo.com')
   lstr =  str.split(':')
   ln = list()
   for i in lstr :
      ln.append(i.replace("\\n", "").strip())
   print(ln)
   readfile()
   shellexe(ln)
   writefile()
